package com.Liuhang.Dao;

import com.Liuhang.JDBC.util;
import com.Liuhang.entity.department;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class getDepList {

    public static List list ;
    public static ResultSet rs;


    public static List getList(){
        list = new ArrayList();
        String sql = "select * from t_dep order by pid asc";
        try {
            Statement st = util.getStatement();
            rs = st.executeQuery(sql);
            while (rs.next()){
                System.out.println("id = "+rs.getObject("pid"));
                System.out.println("deparment ="+rs.getString("dep_name"));
                department dep = new department();

                dep.setPid(rs.getInt("pid"));
                dep.setDep_name(rs.getString("dep_name"));

                list.add(dep);

            }
            for (int i = 0; i <list.size() ; i++) {
                department d1 = (department) list.get(i);
                System.out.println(d1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return list;
    }

    public static int getDep_num(int dep_id){
        String sql = "select id from t_emp where dep_id= "+dep_id+"; ";
        int dep_num = 0;
        Statement st = null;
        try {
            st = util.getStatement();
            rs = st.executeQuery(sql);
            while (rs.next())
                dep_num++;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return dep_num;
    }
}
